<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Note;
use App\Note_Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class NoteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $uid = 0, $zid = 0)
    {
        $note = Note::with(['user:id,nickname,avatar', 'zone:id,title', 'comment.user:id,nickname,avatar'])->latest('create_time');
        if ($request->uid) {
            return $note->where('user_id', $request->uid)->Paginate(6);
        } else if ($request->zid) {
            return $note->where('zone_id', $request->zid)->Paginate(10);
        } else {
            return $note->Paginate(10);
        }

    }

    public function upload(Request $request, $uid)
    {
        $file = $request->file('file');
        $clientName = $_FILES['file']['name'];
        $extension = explode('.', $clientName);
        $newName = md5(date('ymdhis') . $clientName) . "." . $extension[1];
        $file->move("storage/note/{$uid}", $newName);
        return Str::start($newName, "http://craftsman.test/storage/note/{$uid}/");
    }

    public function deleteImg(Request $request)
    {
        $oldFile = Str::after($request->url, "http://craftsman.test/storage/");
        Storage::delete(Str::finish("public/", $oldFile));
        return "ok";
    }

    public function commentIndex(Request $request)
    {
        if ($request->user) {
            return Note::with('comment.user:id,nickname')->latest('create_time')->where('user_id', $request->user)->has('comment')->get();
        }

        return Note_Comment::with('user:id,nickname,avatar', 'note:id,content')->latest('comment_time')->where('note_id', $request->note)->Paginate(6);

    }

    public function addComment(Request $request)
    {
        return Note_Comment::create([
            'user_id' => $request->uid,
            'note_id' => $request->nid,
            'content' => $request->content,
            'comment_time' => now(),
            // 'comment_time' => now()->addHour(8)
        ]);
    }

    public function delComment(Note_Comment $comment)
    {
        if ($comment->delete()) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
            ]);
        }

        return response()->json([
            'status' => 'error',
            'status_code' => 500,
        ]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Note  $note
     * @return \Illuminate\Http\Response
     */
    /**
     * body 带id字段为更新
     * 否则为新建
     */
    public function updateOrCreate(Request $request)
    {
        if ($request->id) { // 更新原记录
            $note = Note::find($request->id)->update([
                'content' => $request->content,
            ]);
            return $note;
        }

        // 创建新纪录
        return Note::create([
            'user_id' => $request->uid,
            'zone_id' => $request->zid,
            'image_src' => $request->image_src,
            'content' => $request->content,
            'create_time' => now(),
        ]);
    }

    public function destroy(Note $note)
    {
        $imglist = collect(json_decode($note->image_src));
        foreach ($imglist->pop() as $value) {
            $oldFile = Str::after($value, "http://craftsman.test/storage/");
            Storage::delete(Str::finish("public/", $oldFile));
        };
        $note->delete();
        return 'OK';
    }
}
